<?php

namespace app\index\controller;
use think\Cookie;
use think\Db;
class Pdfadmin extends Pdf
{

    public $row_info = [];
   /**
     * 公共
     * @author gzy
     * @date   2018年8月12日 09:15:00
     */
    public function _initialize(){
        parent::_initialize();
        if(!Cookie::get('verify_phone') && !in_array($this->request->action(),array("login","check"))){
            //print_r(in_array($this->request->action(),array("login","check")));die;
          $params = $this->request->param();
          if(!$params['type'] || !$params['ids'])  $this->error('参数错误');
            header("Location:".$this->request->domain().'/index/pdfadmin/login/type/'.$params['type'].'/ids/'.$params['ids'].'');
        }else if (Cookie::get('verify_phone')){
          $params = $this->request->param();
         // print_r($this->request->param());die;
          if(!$params['ids'])  $this->error('参数错误');
             $where = [
                  'id'=>$params['ids'],
                  'build_type'=>2,
                  'verify_phone'=>Cookie::get('verify_phone'),
               ];
            if(!$this->row_info  =  $this->ModelShop->table('eb_contract')->where($where)->find()){
                Cookie::delete('verify_phone');
                header("Location:".$this->request->domain().'/index/pdfadmin/login/type/'.$params['type'].'/ids/'.$params['ids'].'');
            }
            switch ($this->row_info['status']) {
               case 3:
               $this->error('此合同已经失效。','https://shoptest.fc62.com/');
                break;
               case 2:
               if(!in_array($this->request->action(),array("question_success"))){

                 header("Location:".$this->request->domain().'/index/pdfadmin/question_success/ids/'.$params['ids']);
                 exit;
               }
               break;
            }
       }
    }
     public function check($type = null,$ids = null){
         if(!$type || !$ids)  $this->error('参数错误');
          if ($this->request->isPost()){
            $params = $this->request->post();
            if(!$params['mobile']) $this->error('请输入手机号');
             $where = [
                'type'=>$type,
                'id'=>$ids,
                'build_type'=>2,
                'verify_phone'=>$params['mobile'],
             ];
            if(!$row =  $this->ModelShop->table('eb_contract')->where($where)->find()){
                $this->error('手机号验证失败,无法获取验证码');
            }else{
               $this->success('手机号验证成功');
            }
          
        }else{
           $this->error(__('参数错误'));
        }


    }
   public function login($type = null,$ids = null){
         if(!$type || !$ids)  $this->error('参数错误');
         $info =  $this->ModelShop->table('eb_contract')->where(['id'=>$ids])->find();
         if(!$info) $this->error('数据不存在');
         if($info['status'] == 3) $this->error('此合同已经失效。','https://shoptest.fc62.com/');
         if ($this->request->isPost()){
             $params = $this->request->post();
            if(!$params['mobile']) $this->error('请输入手机号');
            $where = [
                  'type'=>$type,
                  'id'=>$ids,
                  'build_type'=>2,
                  'verify_phone'=>$params['mobile'],
               ];
            if(!$row =  $this->ModelShop->table('eb_contract')->where($where)->find()){
                $this->error('手机号验证失败,请输入正确的手机号');
            }

          Cookie::set('verify_phone',$params['mobile']);
          $this->success('认证成功');
        }else{
          $this->view->assign("type", $type);
          $this->view->assign("ids", $ids);
          return $this->view->fetch();
        }
    }
    /**
     * 生成 https://oa.fc62.com/index/pdf/generate2/type/2/userid/4/order_id/86/product_id/8/key/1
     */
    public function generate($type = null,$ids= null){
       if(!$type || !$ids)  $this->error('参数错误');
        $row = $this->row_info;
        if(!$row) $this->error('数据不存在');
       if ($this->request->isPost()){
            $params = $this->request->post();
            Db::startTrans();
            if($params){
                $params['closing_date'] = strtotime(date('2021-08-07'));
                $params['sign_date'] = time();
                $params['updatetime'] = time();
                 $info = $params;
                 $info['edition_txt'] = $row['edition_txt'];
                
                 $info['subject'] = $row['subject'];
                 $info['medium'] = $row['medium'];
                 $info['order_sn'] = $row['order_sn'];
                 $info['province_text'] = $info['province']? $this->getProvinceOrderShortList[$info['province']]:'';
                 $info['city_text'] = $info['city']?$this->getProvinceOrderShortList[$info['city']]:'';
                 $info['area_text'] = $info['area']?$this->getProvinceOrderShortList[$info['area']]:'';
                if(!$row['contract_no']){
                  // print_r($info);die;
                   if(!$params['contract_no'] = GenerateContractNo($info)) $this->error('生成合同号失败','https://shoptest.fc62.com/');
                 }else{
                   $info['contract_no'] = $row['contract_no']??'';
                  if(!GenerateContractNo($info,'464','edit')) $this->error('保存信息失败！');
                 }
                 $result = $this->ModelShop->table('eb_contract')->where('id', $row['id'])->update($params);
                 if($result){
                      Db::commit();
                      $this->success('操作成功');
                  }else{
                      Db::rollback();
                  }
            }
            Db::rollback();
            $this->error(__('Parameter %s can not be empty', ''));
       }
         //print_r($row);die;
       if($row){ 
          $row['address_text'] = '';
          $row['province_text'] = $row['province']?$this->getProvinceOrderList[$row['province']]:'';
          $row['city_text'] = $row['city']?$this->getProvinceOrderList[$row['city']]:'';
          $row['area_text'] = $row['area']?$this->getProvinceOrderList[$row['area']]:'';
          if($row['province_text'] && $row['city_text'] && $row['area_text'])
          $row['address_text'] = $row['province_text'].' '.$row['city_text'].' '.$row['city_text'];
        }
       $this->view->assign("row", $row);
       $this->view->assign("urlpdf", '/pdf/'.$this->type_img[$type]['name1']);
       $this->view->assign("type", $type);
       $this->view->assign("ids", $ids);
       return $this->view->fetch();
  }  
   /**
     * 生成
     */
    public function handwriting($ids=null){
       if(!$ids)  $this->error('参数错误');
       $row =  $this->row_info;
       if(!$row) $this->error('数据不存在');
       if ($this->request->isPost()){
            $params = $this->request->post();
            $delinfo = $info = $row;
            $info['province_text'] = $this->getProvinceOrderList[$info['province']];
            $info['city_text'] = $this->getProvinceOrderList[$info['city']];
            $info['area_text'] = $this->getProvinceOrderList[$info['area']];
            $info['gender_text'] = $this->getGenderList[$info['gender']];
            $info['sign_url'] = $params['sign_url'];
            Db::startTrans();
            if ($params) {
                  if($info['email']) unset($info['email']);
                   if($info['code']) unset($info['code']);
                  $info['repeat_student'] = "2020";
                   $resu = $this->watermark($info,1);
                   if($resu['true'] == 1){
                       $params['status'] = 2;
                       $params['contract_url'] = $resu['data'];
                       /*if(isset($info['contract_no'])) unset($info['contract_no']);*/
                       $resu = $this->watermark($info,2);
                       if($resu['true'] != 1){
                            Db::rollback();
                            $this->error('操作失败');
                       }
                       $params['contract_all_url'] = $resu['data'];
                       $resu = $this->watermark($info,3);
                       if($resu['true'] != 1){
                            Db::rollback();
                            $this->error('操作失败');
                       }
                       $params['contract_blank_url'] = $resu['data'];
                       $params['updatetime'] = time();
                       $result = $this->ModelShop->table('eb_contract')->where('id', $ids)->update($params);
                        if($result){
                            DeleteContractFile($delinfo);
                            Db::commit();
                            $this->success('操作成功');
                        }else{
                            Db::rollback();
                            $this->error('操作失败');
                        }
                   }else{
                      Db::rollback();
                      $this->error('操作失败');
                   }
           
           }
            Db::rollback();
           $this->error('操作失败');
       }
        $this->view->assign("ids", $ids);
       return $this->view->fetch();
   }
     /**
     * 生成
     */
    public function question_success($ids=null){
       if(!$ids)  $this->error('参数错误');
       $row =  $this->row_info;
       if(!$row) $this->error('数据不存在');
       if($row['status'] == 1) $this->error('您还没有提交信息',  "/index/pdfadmin/generate/type/".$row['type']."/ids/".$ids."");
       if($row['send_msg'] == 1){
         /*发短信隐藏。*/
         $row['tel'] = $row['phone'];
         write_log([],'',2,'contract_log');
         $result = send_message(35,['parent_name'=>$row['parent_name'],'contract_no'=>$row['contract_no']], [$row],'contract_log');
         $params = [];
         $params['updatetime'] = time();
         $params['send_msg'] = 2;
         $this->ModelShop->table('eb_contract')->where('id', $ids)->update($params);
       }
       $this->view->assign("row", $row);
       return $this->view->fetch();
   }
   
  

    
   
  

}
